﻿function ColumnSortHandler() {
	var _this = this;
	var up_cls = 'ui-icon-arrowthick-1-n', down_cls = 'ui-icon-arrowthick-1-s', losange = 'ui-icon-carat-2-n-s';
	
	this.previousColumn = null;
	
	this.sortColumn = function(head, sortDirection) {
		var nodes = [_this.grid.root].concat(_this.grid.getAllNodes()), column = _this.grid.getColumnDefinition(head);
		for (var i=0;i < nodes.length;i++) {
			nodes[i].children.sort(function(n1, n2) {
				var v1 = n1.record[column.field], v2 = n2.record[column.field];
				return sortDirection * (v1 == v2 ? 0: (v1 < v2 ? -1: 1));
			});
		}
		
		var tbody = _this.grid.getBody().find('tbody').get(0);
		var sortTable = function(node) {
			for (var i=0;i < node.children.length;i++) {
				var child = node.children[i];
				if (child.row != null) tbody.appendChild(child.row);
				sortTable(child);
			}
		}
		
		sortTable(_this.grid.root);
		_this.grid.pageChanged.trigger();
		
		if (_this.previousColumn != null) {
			$(_this.previousColumn).find('.ui-icon').removeClass([up_cls, down_cls]);
			$([_this.previousColumn, head]).toggleClass('ui-state-active');
		}
		$(head).find('.ui-icon').removeClass([losange, up_cls, down_cls].join(' ')).addClass(sortDirection == 1 ? up_cls: down_cls);
		$(_this.grid.getColumnCells(head)).addClass('image-glass');
		
		_this.previousColumn = head;
		_this.state = {column: column.field};
		_this.stateChanged.trigger();
	};
	
	this.onPageContentChangedResort = function(originator) {
		if (originator != _this && _this.previousColumn != null) 
			_this.sortColumn(_this.previousColumn, $(_this.previousColumn).attr('sortdirection'));
	};
	
	this.onColumnClicked = function(e) {
		var col = $(getFirstAncestorOfType(e.target, 'TH'));
		var d = col.attr('sortdirection') != 1 ? 1: -1;
		col.attr('sortdirection', d);
		_this.sortColumn(col.get(0), d);
		
	};
	this.onColumnHover = function(e) {
		var col = $(getFirstAncestorOfType(e.target, 'TH'));
		col.toggleClass('ui-state-default ui-state-hover');
	};
	this.detach = function() {
		_this.grid.getHeaderCells().each(function() {
			var col = $(this);
			col.removeClass('dt-table-sortable');
			col.find('.dt-sort-button').remove();
			col.unbind('click', _this.onColumnClicked);
			col.unbind('mouseenter mouseleave', _this.onColumnHover);
		});
		
		if (_this.previousColumn != null) {
			$(_this.previousColumn).removeClass('ui-state-active');
			$(_this.grid.getColumnCells(_this.previousColumn).concat([_this.previousColumn])).removeClass('image-glass');
		}
		
		_this.grid.pageContentChanged.remove(this.onPageContentChangedResort);
	};

	this.attach = function(grid) {
		this.grid = grid;
		this.grid.getHeaderCells().each(function() {
			var col = $(this);
			col.bind('mouseenter mouseleave', _this.onColumnHover);
			
			if (_this.grid.getColumnDefinition(this).sortable) {
				col.addClass('dt-table-sortable');

				col.find('.dt-table-value').append(
					$('<div>').addClass('dt-sort-button ui-icon '+ losange).css('display', 'inline-block')
				);
				col.bind('click', _this.onColumnClicked);
			}
		});
		
		_this.grid.pageContentChanged.add(this.onPageContentChangedResort);
	};
}; ColumnSortHandler.inherits(DatagridPlugin);